package com.ykh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.config.Cache;
import com.ykh.config.LogAnnotation;
import com.ykh.pojo.dto.PageParams;
import com.ykh.pojo.dto.VideoDto;
import com.ykh.pojo.po.User;
import com.ykh.pojo.po.Video;
import com.ykh.pojo.vo.ErrorCode;
import com.ykh.pojo.vo.Result;
import com.ykh.pojo.vo.VideoVo;
import com.ykh.service.VideoService;
import com.ykh.util.UserThreadLocal;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ykh
 * @since 2023年10月26日
 */
@RestController
@RequestMapping("/video")
public class VideoController {
    @Autowired
    VideoService videoService;

    @ApiOperation("上传视频")
    @PostMapping("/upload")
//    @LogAnnotation(module = "视频", operation = "上传视频")
    public Result uploadVideo(VideoDto videoDto) {

        if(videoDto.getVideoFile() == null){
            return Result.fail(ErrorCode.VIDEO_UPLOAD_FAIL.getCode(), ErrorCode.VIDEO_UPLOAD_FAIL.getMsg());
        }
        return videoService.uploadVedio(videoDto);
    }

    @ApiOperation("获取单个视频播放链接")
    @GetMapping("/get")
    @Cache(expire = 1*1000, name = "getVideo")
    @LogAnnotation(module = "视频", operation = "获取单个视频")
    public Result getVideo(@RequestParam Integer videoId) throws UnsupportedEncodingException {
        return videoService.getVideo(videoId);
    }

    @PostMapping("/list")
    @ApiOperation("分页获取视频")
//    @Cache(expire = 1*60*1000, name = "getVideoList")
    @LogAnnotation(module = "视频模块", operation = "分页获取视频")
    public Result getVideoList(@RequestBody PageParams pageParams) {
        return Result.success(videoService.getVideoList(pageParams));
    }

    @PostMapping("/good")
    @ApiOperation("用户点赞视频")
    public Result goodVideo(@RequestParam Integer videoId) {
        return videoService.goodVideo(videoId);
    }

    /**
     * 根据用户id分页获取作品接口
     * @return
     */
    @ApiOperation("用户获取自己视频接口")
    @GetMapping("/info")
    @LogAnnotation(module = "视频", operation = "用户获取自己视频")
    public Result getUserVideoList(PageParams pageParams){
        User user = UserThreadLocal.get();
        if(user == null){
            return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        return Result.success(videoService.getUserVideoList(pageParams, user.getAccount()));
    }

    /**
     * 根据分类分页获取作品接口
     * @return
     */
    @ApiOperation("根据分类分页获取作品接口")
    @GetMapping("/category")
    @Cache(expire = 1*60*1000, name = "getVideoListByCategory")
    @LogAnnotation(module = "视频模块", operation = "根据分类分页获取作品")
    public Result getVideoListByCategory(PageParams pageParams, Integer categoryId){
        List<VideoVo> videoListByCategory = videoService.getVideoListByCategory(pageParams, categoryId);
        if(videoListByCategory==null){
            Result.fail(ErrorCode.NO_EXIST_CATEGORY_VIDEO.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        return Result.success(videoListByCategory);
    }

    /**
     * 获取视频总页数接口
     * @return
     */
    @ApiOperation("获取视频总页数接口")
    @GetMapping("/total/page")
    @Cache(expire = 10*60*1000, name = "getVideoTotalPage")
    @LogAnnotation(module = "视频", operation = "获取视频分页的总页数")
    public Result getVideoTotalPage(PageParams pageParams){
        Long totalPage = videoService.getVideoTotalPage(pageParams);
        return Result.success(totalPage);
    }

    /**
     * 用户删除自己视频接口
     * @return
     */
    @ApiOperation("用户删除视频接口")
    @PostMapping("/delete")
    @LogAnnotation(module = "视频", operation = "delete")
    public Result delete(@RequestParam Integer videoId){
        User user = UserThreadLocal.get();
        if(user == null){
            return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        return videoService.delete(videoId, user);
    }
    /**
     * 热门视频接口
     * @return
     */
    @ApiOperation("热门视频接口")
    @GetMapping("/hot")
    @Cache(expire = 1*60*1000, name = "hot")
    @LogAnnotation(module = "视频", operation = "热门视频")
    public Result hot(@RequestParam @ApiParam("要热门视频条数") Integer size){
        return Result.success(videoService.getHotVideoList(size));
    }


    /**
     * 获取所有视频id
     * @return
     */
    @ApiOperation("获取所有视频id")
    @GetMapping("/all")
//    @Cache(expire = 1*60*1000, name = "hot")
//    @LogAnnotation(module = "视频", operation = "热门视频")
    public Result getAllVideoId(){
        return Result.success(videoService.getAllVideo());
    }



}

